package com.example.scale;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;

import com.example.scale.SceneManager.SceneType;

public class MainMenuScene extends BaseScene implements
		IOnMenuItemClickListener {

	private MenuScene menuChildScene;
	private final int MENU_PLAY = 0;
	private final int MENU_OPTIONS = 1;
	private final int MENU_SKILLS = 2;

	@Override
	public void createScene() {
		createBackground();
		createMenuChildScene();
	}

	@Override
	public void onBackKeyPressed() {
		ResourcesManager.getInstance().unloadMenuTextures();
		System.exit(0);
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_MENU;
	}

	@Override
	public void disposeScene() {
		
	}

	// Create a new sprite in the middle of the screen for background
	// And enable dithering to improve gradient quality
	private void createBackground() {
		attachChild(new Sprite(GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT/2,
				resourcesManager.menu_background_region, vbom) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		});
	}

	private void createMenuChildScene() {
		menuChildScene = new MenuScene(camera);
		menuChildScene.setPosition(0, 0);

		final IMenuItem playMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_PLAY, resourcesManager.play_region,
						vbom), 1f, 0.9f);
		final IMenuItem optionsMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_OPTIONS,
						resourcesManager.options_region, vbom), 1f, 0.9f);
		final IMenuItem skillsMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_SKILLS,
						resourcesManager.skills_region, vbom), 1f, 0.9f);

		menuChildScene.addMenuItem(playMenuItem);
		menuChildScene.addMenuItem(optionsMenuItem);
		menuChildScene.addMenuItem(skillsMenuItem);

		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);

		
		playMenuItem.setPosition(GameActivity.CAMERA_WIDTH/6, 115);
		optionsMenuItem.setPosition(5*GameActivity.CAMERA_WIDTH/6, 115);
		skillsMenuItem.setPosition(GameActivity.CAMERA_WIDTH/2, 115);
		
		menuChildScene.setOnMenuItemClickListener(this);

		setChildScene(menuChildScene);
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch (pMenuItem.getID()) {
		case MENU_PLAY:
			// Load game scene
			//SceneManager.getInstance().loadGameScene(engine);
			SceneManager.getInstance().loadLevelSelectScene();
			return true;
		case MENU_OPTIONS:
			SceneManager.getInstance().loadOptionsScene();
			return true;
		case MENU_SKILLS:
			SceneManager.getInstance().loadSkillsScene();
			return false;
		default:
			return false;
		}
	}

}
